﻿#pragma warning disable 0168
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Celeriq.Utilities
{
	/// <summary />
	public static class ObjectHelper
	{
		/// <summary>
		/// Determines the amount of memory an object uses
		/// </summary>
		public static long SizeOf(object o)
		{
			if (o == null) return 0;
			var fileName = Path.GetTempFileName();
			try
			{
				using (var fs = File.Create(fileName))
				{
					var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
					formatter.Serialize(fs, o);
					return fs.Length;
				}
			}
			catch (Exception ex)
			{
				return - 1;
			}
			finally
			{
				if (File.Exists(fileName))
					File.Delete(fileName);
			}
		}
	}
}
